package com.promise.jokerdream.ui.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import com.promise.jokerdream.ui.theme.AppColors
import com.promise.jokerdream.ui.theme.AppFontSizes

/**
 * 通用对话框组件
 * 支持自定义标题、Composable 内容、按钮文字和颜色
 */
@Composable
    fun PMDialog(
    show: Boolean,
    title: String,
    confirmText: String = "确认",
    cancelText: String = "取消",
    confirmButtonColor: Color = AppColors.Success, // 绿色
    cancelButtonColor: Color = AppColors.Gray500, // 灰色
    onDismiss: () -> Unit,
    onConfirm: () -> Unit,
    content: @Composable () -> Unit
) {
    if (show) {
        Dialog(onDismissRequest = onDismiss) {
            Card(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(12.dp),
                shape = RoundedCornerShape(12.dp),
                colors = CardDefaults.cardColors(
                    containerColor = MaterialTheme.colorScheme.surface
                )
            ) {
                Column(
                    modifier = Modifier.fillMaxWidth()
                ) {
                    // 标题
                    Text(
                        text = title,
                        fontSize = AppFontSizes.h3,
                        fontWeight = FontWeight.Bold,
                        textAlign = TextAlign.Center,
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(20.dp, 16.dp, 20.dp, 8.dp)
                    )
                    
                    // 内容
                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(20.dp, 0.dp, 20.dp, 16.dp),
                        contentAlignment = Alignment.Center
                    ) {
                        content()
                    }
                    
                    // 分割线
                    HorizontalDivider(
                        color = MaterialTheme.colorScheme.outline.copy(alpha = 0.5f),
                        thickness = 0.5.dp
                    )
                    
                    // 按钮区域
                    Row(
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        // 左按钮（取消）
                        TextButton(
                            onClick = onDismiss,
                            modifier = Modifier
                                .weight(1f)
                                .height(48.dp),
                            contentPadding = PaddingValues(0.dp)
                        ) {
                            Text(
                                cancelText,
                                color = cancelButtonColor,
                                fontWeight = FontWeight.Medium
                            )
                        }
                        
                        // 中间分割线
                        Box(
                            modifier = Modifier
                                .width(0.5.dp)
                                .height(48.dp)
                                .background(MaterialTheme.colorScheme.outline.copy(alpha = 0.5f))
                        )
                        
                        // 右按钮（确认）
                        TextButton(
                            onClick = onConfirm,
                            modifier = Modifier
                                .weight(1f)
                                .height(48.dp),
                            contentPadding = PaddingValues(0.dp)
                        ) {
                            Text(
                                confirmText,
                                color = confirmButtonColor,
                                fontWeight = FontWeight.Medium
                            )
                        }
                    }
                }
            }
        }
    }
}

/**
 * 便捷的字符串内容对话框
 * 用于简单的文本确认对话框
 */
@Composable
fun PMAlertDialog(
    show: Boolean,
    title: String,
    content: String,
    confirmText: String = "确认",
    cancelText: String = "取消",
    confirmButtonColor: Color = AppColors.Success,
    cancelButtonColor: Color = AppColors.Gray500,
    onDismiss: () -> Unit,
    onConfirm: () -> Unit
) {
    PMDialog(
        show = show,
        title = title,
        confirmText = confirmText,
        cancelText = cancelText,
        confirmButtonColor = confirmButtonColor,
        cancelButtonColor = cancelButtonColor,
        onDismiss = onDismiss,
        onConfirm = onConfirm
    ) {
        Text(
            text = content,
            fontSize = AppFontSizes.mediumBody,
            textAlign = TextAlign.Center,
            modifier = Modifier.fillMaxWidth()
        )
    }
}

/**
 * 危险操作确认对话框
 * 专门用于删除等危险操作，确认按钮为红色
 */
@Composable
fun DangerAlertDialog(
    show: Boolean,
    title: String,
    content: String,
    confirmText: String = "删除",
    cancelText: String = "取消",
    onDismiss: () -> Unit,
    onConfirm: () -> Unit
) {
    PMAlertDialog(
        show = show,
        title = title,
        content = content,
        confirmText = confirmText,
        cancelText = cancelText,
        confirmButtonColor = Color.Red,
        cancelButtonColor = AppColors.Gray500,
        onDismiss = onDismiss,
        onConfirm = onConfirm
    )
}
